package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import com.pallas.swing.date.DateComboBox;

import controller.MainController;
import exceptions.MyException;

import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;

import model.Anwendung;
import model.Patient;
import model.Raum;
import model.Termin;
import model.Therapeut;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

public class TerminAnlegenDialog extends JDialog {

	private boolean done;
	private MainController mainController;
	private int terminId;

	private final JPanel panelOben = new JPanel();
	private DateComboBox dateComboBoxDatum;
	private JComboBox comboBoxUhrzeit;
	private JComboBox comboBoxAnwendung;
	private JComboBox comboBoxTherapeut;
	private JComboBox comboBoxRaum;
	private DefaultListModel modelPatienten;
	private JList listPatienten;

	/**
	 * Create the dialog.
	 */
	public TerminAnlegenDialog(JDialog owner, MainController mainController, int terminId) {
		super(owner, true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Termin anlegen/\u00E4ndern");
		setBounds(100, 100, 450, 400);
		done = false;
		this.mainController = mainController;
		this.terminId = terminId;
		
		getContentPane().setLayout(new BorderLayout());
		panelOben.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panelOben, BorderLayout.CENTER);
		panelOben.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		{
			JLabel labelDatum = new JLabel("Datum");
			panelOben.add(labelDatum, "2, 2, right, default");
		}
		{
			dateComboBoxDatum = new DateComboBox(new Date(), "dd.MM.yyyy");
			panelOben.add(dateComboBoxDatum, "4, 2, fill, default");
		}
		{
			JLabel labelUhrzeit = new JLabel("Uhrzeit");
			panelOben.add(labelUhrzeit, "2, 4, right, default");
		}
		{
			String[] comboBoxUhrzeitData = {
					"08:00", "08:20", "08:40", 
					"09:00", "09:20", "09:40", 
					"10:00", "10:20", "10:40", 
					"11:00", "11:20", "11:40", 
					"13:00", "13:20", "13:40", 
					"14:00", "14:20", "14:40", 
					"15:00", "15:20", "15:40", 
					"16:00", "16:20", "16:40"
			};
			comboBoxUhrzeit = new JComboBox(comboBoxUhrzeitData);
			panelOben.add(comboBoxUhrzeit, "4, 4, fill, default");
		}
		{
			JLabel labelAnwendung = new JLabel("Anwendung");
			panelOben.add(labelAnwendung, "2, 6, right, default");
		}
		{
			comboBoxAnwendung = new JComboBox(this.mainController.getAnwendungsController().getAnwendungen().toArray());
			panelOben.add(comboBoxAnwendung, "4, 6, fill, default");
		}
		{
			JLabel labelTherapeut = new JLabel("Therapeut");
			panelOben.add(labelTherapeut, "2, 8, right, default");
		}
		{
			comboBoxTherapeut = new JComboBox(this.mainController.getBenutzerController().getTherapeuten().toArray());
			panelOben.add(comboBoxTherapeut, "4, 8, fill, default");
		}
		{
			JLabel labelRaum = new JLabel("Raum");
			panelOben.add(labelRaum, "2, 10, right, default");
		}
		{
			comboBoxRaum = new JComboBox(this.mainController.getRaumController().getRaeume().toArray());
			panelOben.add(comboBoxRaum, "4, 10, fill, default");
		}
		{
			JLabel labelPatienten = new JLabel("Patienten");
			panelOben.add(labelPatienten, "2, 12, right, default");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			panelOben.add(scrollPane, "4, 12, fill, fill");
			{
				modelPatienten = new DefaultListModel();
				List<Patient> patienten = this.mainController.getPatientenController().getPatienten();
				
				for (int i = 0; i < patienten.size(); i++) {
					modelPatienten.addElement(patienten.get(i));
				}
				
				listPatienten = new JList(modelPatienten);
				scrollPane.setViewportView(listPatienten);
			}
		}
		{
			JPanel panelUnten = new JPanel();
			panelUnten.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(panelUnten, BorderLayout.SOUTH);
			{
				JButton buttonOK = new JButton("OK");
				buttonOK.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Date datum = dateComboBoxDatum.getDate();
						
						String uhrzeit = (String) comboBoxUhrzeit.getSelectedItem();
						int hours = Integer.parseInt(uhrzeit.substring(0, 2));
						int minutes = Integer.parseInt(uhrzeit.substring(3, 5));
						
						Anwendung anwendung = (Anwendung) comboBoxAnwendung.getSelectedItem();
						String anwendungsBezeichnung = (anwendung == null) ? "" : anwendung.getBezeichnung();
						
						Therapeut therapeut = (Therapeut) comboBoxTherapeut.getSelectedItem();
						int benutzerId = (therapeut == null) ? -1 : therapeut.getBenutzerId();
						
						Raum raum = (Raum) comboBoxRaum.getSelectedItem();
						String raumBezeichnung = (raum == null) ? "" : raum.getBezeichnung();
						
						List<Patient> patienten = listPatienten.getSelectedValuesList();
						int[] patientenIds = new int[patienten.size()];
						
						for (int i = 0; i < patienten.size(); i++) {
							patientenIds[i] = patienten.get(i).getPatientenId();
						}
						
						try {
							if (TerminAnlegenDialog.this.terminId >= 0) {
								TerminAnlegenDialog.this.mainController.getTerminController().updateTermin(TerminAnlegenDialog.this.terminId, datum, hours, minutes, anwendungsBezeichnung, benutzerId, raumBezeichnung, patientenIds);
							}
							else {
								TerminAnlegenDialog.this.mainController.getTerminController().createTermin(datum, hours, minutes, anwendungsBezeichnung, benutzerId, raumBezeichnung, patientenIds);
							}
							
							done = true;
							setVisible(false);
							dispose();
						}
						catch (MyException myException) {
							JOptionPane.showMessageDialog(null, myException.getMessage());
						}
					}
				});
				buttonOK.setActionCommand("OK");
				panelUnten.add(buttonOK);
				getRootPane().setDefaultButton(buttonOK);
			}
			{
				JButton buttonAbbrechen = new JButton("Abbrechen");
				buttonAbbrechen.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						dispose();
					}
				});
				buttonAbbrechen.setActionCommand("Cancel");
				panelUnten.add(buttonAbbrechen);
			}
		}
		
		if (this.terminId >= 0) {
			Termin termin = this.mainController.getTerminController().getTermin(terminId);
			
			dateComboBoxDatum.setDate(termin.getDatum());
			
			DecimalFormat decimalFormat = new DecimalFormat("00");
			comboBoxUhrzeit.setSelectedItem(decimalFormat.format(termin.getHours()) + ":" + decimalFormat.format(termin.getMinutes()));
			
			comboBoxAnwendung.setSelectedItem(termin.getAnwendung());
			
			comboBoxTherapeut.setSelectedItem(termin.getTherapeut());
			
			comboBoxRaum.setSelectedItem(termin.getRaum());
			
			List<Patient> patienten = termin.getPatienten();
			
			for (int i = 0; i < patienten.size(); i++) {
				int currentIndex = modelPatienten.indexOf(patienten.get(i));
				listPatienten.addSelectionInterval(currentIndex, currentIndex);
			}
		}
	}

	public boolean showDialog() {
		setVisible(true);
		return done;
	}

}
